package com.neusoft.neuiotms.dcam.fixed.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.persistence.EntityNotFoundException;

import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.bizcore.web.utils.EasyPoiStylerColorImpl;
import com.neusoft.neuiotms.dcam.fixed.converter.AhAssetCardConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.AhAssetCardDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.AssetDTO;
import com.neusoft.neuiotms.dcam.fixed.model.AhAssetCard;
import com.neusoft.neuiotms.dcam.fixed.repository.AhAssetCardRepository;
import com.neusoft.neuiotms.dcam.fixed.service.AhAssetCardService;
import com.neusoft.neuiotms.dcam.fixed.service.AssetService;
import com.neusoft.neuiotms.dcam.fixed.vo.AhAssetCardExportVO;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

@Service
public class AhAssetCardServiceImpl implements AhAssetCardService {

    @Autowired
    private AhAssetCardRepository ahAssetCardRepository;
    @Autowired
    private AhAssetCardConverter ahAssetCardConverter;
    @Autowired
    private AssetService assetService;

    @Override
    public List<AhAssetCardDTO> search(Searchable searchable) {
        final List<AhAssetCard> list = this.ahAssetCardRepository.search(searchable);
        return this.ahAssetCardConverter.toListDTO(list);
    }

    @Override
    public Page<AhAssetCardDTO> search(Pageable pageable, Searchable searchable) {
        final Page<AhAssetCard> page = this.ahAssetCardRepository.search(pageable, searchable);
        return this.ahAssetCardConverter.toPageDTO(page);
    }

    @Override
    public AhAssetCardDTO create(AhAssetCardDTO dto) {
        final AhAssetCard model = this.ahAssetCardConverter.toModel(dto);
        this.ahAssetCardRepository.save(model);
        return this.ahAssetCardConverter.toDTO(model);
    }

    @Override
    public AhAssetCardDTO update(Long id, AhAssetCardDTO dto) {
        AhAssetCard model;
        try {
            model = this.ahAssetCardRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        dto.setId(model.getId());
        model = this.ahAssetCardConverter.toModel(dto);
        this.ahAssetCardRepository.save(model);

        return this.ahAssetCardConverter.toDTO(model);
    }

    @Override
    public AhAssetCardDTO details(Long id) {
        final AhAssetCard model;
        try {
            model = this.ahAssetCardRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        return this.ahAssetCardConverter.toDTO(model);
    }

    @Override
    public void delete(Long id) {
        this.ahAssetCardRepository.deleteById(id);
    }

    @Override
    public Workbook export(Searchable searchable) {
        final List<AhAssetCard> list = this.ahAssetCardRepository.search(searchable);
        final List<AhAssetCardExportVO> voList =
                list.stream().map(it -> this.ahAssetCardConverter.toVO(it)).collect(Collectors.toList());
        final ExportParams param = new ExportParams("资产卡片导出", "资产卡片导出", ExcelType.XSSF);
        param.setStyle(EasyPoiStylerColorImpl.class);

        return ExcelExportUtil.exportExcel(param, AhAssetCardExportVO.class, voList);
    }

    @Override
    public void associate(long id, List<Long> assets) {
        final AhAssetCard model;
        try {
            model = this.ahAssetCardRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }

        final Searchable searchAsset = new Searchable();
        searchAsset.put("originalCode", model.getCode());
        final List<AssetDTO> oldAssociatedAssets = this.assetService.index(searchAsset);
        final List<AssetDTO> prepareRemoveList = oldAssociatedAssets.stream()
                .filter(it -> assets.stream().filter(k -> k.longValue() == it.getId().longValue()).count() == 0)
                .collect(Collectors.toList());

        for (final Long assetId : assets) {
            this.assetService.updateAseetCardForSL(assetId, model.getCode());
        }
        for (final AssetDTO a : prepareRemoveList) {
            this.assetService.updateAseetCardForSL(a.getId(), "");
        }

    }

}
